package org.example.demo.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import org.example.demo.constant.HintConstant;
import org.example.demo.entity.BackgroundCheck;
import org.example.demo.entity.InformationHint;
import org.example.demo.service.BackgroundCheckService;
import org.example.demo.service.InformationHintService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * 政审信息管理控制器
 */
@RestController
@RequestMapping("/background-check")
public class BackgroundCheckController {

    @Autowired
    private BackgroundCheckService backgroundCheckService;

    @Autowired
    private InformationHintService informationHintService;

    /**
     * 分页查询政审信息
     * 
     * @param pageNum  页码
     * @param pageSize 每页大小
     * @param name     申请人姓名，可为空
     * @param type     政审类型，可为空
     * @param result   政审结果，可为空
     * @return 分页数据
     */
    @PostMapping("/selectPage")
    public Map<String, Object> selectPage(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestBody(required = false) Map<String, Object> params) {

        Map<String, Object> result = new HashMap<>();
        try {
            // 获取查询参数
            String name = params != null ? (String) params.get("name") : null;
            String type = params != null ? (String) params.get("type") : null;
            String resultParam = params != null ? (String) params.get("result") : null;

            // 查询分页数据
            IPage<BackgroundCheck> page = backgroundCheckService.selectPage(pageNum, pageSize, name, type, resultParam);

            Map<String, Object> data = new HashMap<>();
            data.put("page", page);

            result.put("code", 200);
            result.put("msg", "查询成功");
            result.put("data", data);
        } catch (Exception e) {
            result.put("code", 500);
            result.put("msg", "查询失败：" + e.getMessage());
        }
        return result;
    }

    /**
     * 统计政审信息总数
     * 
     * @param name   申请人姓名，可为空
     * @param type   政审类型，可为空
     * @param result 政审结果，可为空
     * @return 总数
     */
    @PostMapping("/selectCount")
    public Map<String, Object> selectCount(@RequestBody(required = false) Map<String, Object> params) {
        Map<String, Object> result = new HashMap<>();
        try {
            // 获取查询参数
            String name = params != null ? (String) params.get("name") : null;
            String type = params != null ? (String) params.get("type") : null;
            String resultParam = params != null ? (String) params.get("result") : null;

            // 查询总数
            int count = backgroundCheckService.selectCount(name, type, resultParam);

            result.put("code", 200);
            result.put("msg", "查询成功");
            result.put("data", count);
        } catch (Exception e) {
            result.put("code", 500);
            result.put("msg", "查询失败：" + e.getMessage());
        }
        return result;
    }

    /**
     * 根据ID查询政审信息
     * 
     * @param id 政审信息ID
     * @return 政审信息
     */
    @GetMapping("/getById")
    public Map<String, Object> getById(@RequestParam Long id) {
        Map<String, Object> result = new HashMap<>();
        try {
            BackgroundCheck backgroundCheck = backgroundCheckService.getById(id);

            if (backgroundCheck != null) {
                result.put("code", 200);
                result.put("msg", "查询成功");
                result.put("data", backgroundCheck);
            } else {
                result.put("code", 404);
                result.put("msg", "未找到对应的政审记录");
            }
        } catch (Exception e) {
            result.put("code", 500);
            result.put("msg", "查询失败：" + e.getMessage());
        }
        return result;
    }

    /**
     * 新增或更新政审信息
     * 
     * @param backgroundCheck 政审信息
     * @return 操作结果
     */
    @PostMapping("/saveOrUpdate")
    public Map<String, Object> saveOrUpdate(@RequestBody BackgroundCheck backgroundCheck) {
        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = backgroundCheckService.saveOrUpdate(backgroundCheck);

            if (success) {
                result.put("code", 200);

                if (backgroundCheck.getId() == null) {
                    InformationHint informationHint = InformationHint.builder()
                            .userId(backgroundCheck.getApplicantId())
                            .title(HintConstant.BACKGROUND_CHECK_HINT)
                            .content(backgroundCheck.getType())
                            .time(backgroundCheck.getStartDate())
                            .build();
                    informationHintService.save(informationHint);
                }
                result.put("msg", backgroundCheck.getId() == null ? "新增成功" : "更新成功");
            } else {
                result.put("code", 500);
                result.put("msg", backgroundCheck.getId() == null ? "新增失败" : "更新失败");
            }
        } catch (Exception e) {
            result.put("code", 500);
            result.put("msg", "操作失败：" + e.getMessage());
        }
        return result;
    }

    /**
     * 删除政审信息
     * 
     * @param id 政审信息ID
     * @return 操作结果
     */
    @DeleteMapping("/delete")
    public Map<String, Object> delete(@RequestParam Long id) {
        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = backgroundCheckService.removeById(id);

            if (success) {
                result.put("code", 200);
                result.put("msg", "删除成功");
            } else {
                result.put("code", 500);
                result.put("msg", "删除失败");
            }
        } catch (Exception e) {
            result.put("code", 500);
            result.put("msg", "删除失败：" + e.getMessage());
        }
        return result;
    }

    /**
     * 统计各类政审结果的数量
     * 
     * @return 统计结果
     */
    @GetMapping("/countByResults")
    public Map<String, Object> countByResults() {
        Map<String, Object> result = new HashMap<>();
        try {
            Map<String, Integer> stats = backgroundCheckService.countByResults();

            result.put("code", 200);
            result.put("msg", "查询成功");
            result.put("data", stats);
        } catch (Exception e) {
            result.put("code", 500);
            result.put("msg", "查询失败：" + e.getMessage());
        }
        return result;
    }
}